<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>BehaviorTree | Abel&#39;Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="在制作游戏里面，除了使用FSM，还有一种实现模式：行为树。编写本文用于梳理行为树中的概念，知识点。里面使用到了三方库来编辑行为树(behavior3.com)。行为树主要用于做系统控制的，能通过简单的节点组合，编辑出复杂的逻辑。 概述节点1. composite组合节点，可以连接多个子节点，用于决策行为树走向。 1.1. Sequence &amp;Sequence 节点 顺序执行子节点，当子节点">
<meta property="og:type" content="article">
<meta property="og:title" content="BehaviorTree">
<meta property="og:url" content="http://example.com/2020/12/15/2020/2020-12-15-BehaviorTree/index.html">
<meta property="og:site_name" content="Abel&#39;Blog">
<meta property="og:description" content="在制作游戏里面，除了使用FSM，还有一种实现模式：行为树。编写本文用于梳理行为树中的概念，知识点。里面使用到了三方库来编辑行为树(behavior3.com)。行为树主要用于做系统控制的，能通过简单的节点组合，编辑出复杂的逻辑。 概述节点1. composite组合节点，可以连接多个子节点，用于决策行为树走向。 1.1. Sequence &amp;Sequence 节点 顺序执行子节点，当子节点">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-12-14T16:00:00.000Z">
<meta property="article:modified_time" content="2021-07-28T09:13:30.224Z">
<meta property="article:author" content="Abel Sean">
<meta property="article:tag" content="Game">
<meta property="article:tag" content="AI">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Abel&#39;Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Abel&#39;Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-2020/2020-12-15-BehaviorTree" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/12/15/2020/2020-12-15-BehaviorTree/" class="article-date">
  <time datetime="2020-12-14T16:00:00.000Z" itemprop="datePublished">2020-12-15</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      BehaviorTree
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>在制作游戏里面，除了使用FSM，还有一种实现模式：行为树。编写本文用于梳理行为树中的概念，知识点。里面使用到了三方库来编辑行为树(behavior3.com)。行为树主要用于做系统控制的，能通过简单的节点组合，编辑出复杂的逻辑。</p>
<h1 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h1><h1 id="节点"><a href="#节点" class="headerlink" title="节点"></a>节点</h1><h2 id="1-composite"><a href="#1-composite" class="headerlink" title="1. composite"></a>1. composite</h2><p>组合节点，可以连接多个子节点，用于决策行为树走向。</p>
<h3 id="1-1-Sequence-amp"><a href="#1-1-Sequence-amp" class="headerlink" title="1.1. Sequence &amp;"></a>1.1. Sequence &amp;</h3><p>Sequence 节点 顺序执行子节点，当子节点返回b3.SUCCESS直接访问下一个子节点，直到所有子节点都返回b3.SUCCESS，才从当前节点返回b3.SUCCESS，当子节点返回b3.FAILURE，则直接从当前节点返回b3.FAILURE 类似于逻辑与&amp;的概念。</p>
<h3 id="1-2-Priority"><a href="#1-2-Priority" class="headerlink" title="1.2. Priority |"></a>1.2. Priority |</h3><p>Priority 节点 选择执行子节点，当子节点返回b3.FAILURE直接访问下一个子节点，直到所有子节点都返回b3.FAILURE，才从当前节点返回b3.FAILURE，当子节点返回b3.SUCCESS，则直接从当前节点返回b3.SUCCESS 类似于逻辑或|的概念。</p>
<h3 id="1-3-MemSequence-amp"><a href="#1-3-MemSequence-amp" class="headerlink" title="1.3. MemSequence &amp;"></a>1.3. MemSequence &amp;</h3><p>MemSequence 节点 逻辑和Sequence一样，但是这里涉及到一个b3.RUNNING的返回值，就是子节点并不知道执行结果是成功还是失败，需要等后续才知道是否成功。其实就是异步的节点。</p>
<p>对于Sequence节点，当它再次遍历子节点的时候，还是从第一个子节点开始遍历，但是MemSequence却会记住之前在Running的子节点,然后下次遍历的时候，直接从Running的那个子节点开始，这可以很方便的继续之前中断的逻辑决策。</p>
<p>比如一个怪物，它的技能有3段，就可以通过这样的节点来组织。</p>
<h3 id="1-4-MemPriority"><a href="#1-4-MemPriority" class="headerlink" title="1.4. MemPriority |"></a>1.4. MemPriority |</h3><p>可以类似于Priority，加上了Mem前缀的意义，已经在1.3. 章节论述过了。</p>
<h2 id="2-decorators"><a href="#2-decorators" class="headerlink" title="2. decorators"></a>2. decorators</h2><p>装饰节点， 可以连接一个子节点， 用于修饰子节点的返回值</p>
<h3 id="2-1-Repeator"><a href="#2-1-Repeator" class="headerlink" title="2.1. Repeator"></a>2.1. Repeator</h3><p>Repeator 节点，重复执行子节点maxLoop次</p>
<h3 id="2-2-RepeatUntilFailure"><a href="#2-2-RepeatUntilFailure" class="headerlink" title="2.2. RepeatUntilFailure"></a>2.2. RepeatUntilFailure</h3><p>RepeatUnitilFailure 节点 ，重复执行子节点直到子节点返回Failure</p>
<h3 id="2-3-RepeatUntilSuccess"><a href="#2-3-RepeatUntilSuccess" class="headerlink" title="2.3. RepeatUntilSuccess"></a>2.3. RepeatUntilSuccess</h3><p>RepeatUntilSuccess 节点，重复执行子节点直到子节点返回Success</p>
<h2 id="3-condition"><a href="#3-condition" class="headerlink" title="3. condition"></a>3. condition</h2><p>条件节点， 可以用于做出判断并返回不同的值</p>
<h2 id="4-action"><a href="#4-action" class="headerlink" title="4. action"></a>4. action</h2><p>行为节点， 可以执行行为。</p>
<h1 id="行为树应用"><a href="#行为树应用" class="headerlink" title="行为树应用"></a>行为树应用</h1><p>通过一段时间的实践，行为树只负责一些行为模式的切换做支持。</p>
<p>行为模式的切换，只通过一些阈值，事件来驱动做一些策略的修改。</p>
<p>行为树的tick时间限制在1sec一次。</p>
<p>行为模式是直接通过程序来实现，可以制作出来的效果可以让怪物灵巧很多。</p>
<p>在制作的时候，通过4个方面去分析问题：</p>
<ol>
<li><p>分析怪物一共有多少个<strong>行为模式</strong></p>
</li>
<li><p>需要抽象多少种怪物属性提供给行为树；</p>
</li>
<li><p>需要抽象多少种事件提供给行为树；</p>
</li>
<li><p>设计每个行为模式的工作细节，方便今后boss重复使用；</p>
</li>
</ol>
<p>怪物在走的时候，需要增加一些细节表现，防止太生硬。比如，待机的时候，会看一看玩家。而不是那种只会走路的那种。</p>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li>[1] <a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control)">Wiki行为树</a></li>
<li>[2] <a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E8%A1%8C%E4%B8%BA%E6%A0%91/22735785?fr=aladdin">百度百科行为树</a></li>
<li>[3] <a target="_blank" rel="noopener" href="https://github.com/behavior3/behavior3editor">behavior3editor_github</a></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/12/15/2020/2020-12-15-BehaviorTree/" data-id="ckrn3an79001720xw1zi79v0d" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/AI/" rel="tag">AI</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Game/" rel="tag">Game</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/12/24/2020/2020-12-24-cgo%E4%BD%BF%E7%94%A8/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          cgo的使用
        
      </div>
    </a>
  
  
    <a href="/2020/12/15/2020/2020-12-15-go%E8%AF%AD%E8%A8%80%E5%B9%B6%E5%8F%91%E4%B9%8B%E9%81%93%E7%AC%94%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">go语言并发之道笔记</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/3D/" rel="tag">3D</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Game/" rel="tag">Game</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Go/" rel="tag">Go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Math/" rel="tag">Math</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Server/" rel="tag">Server</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Unreal-Engine4/" rel="tag">Unreal Engine4</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/golang/" rel="tag">golang</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/guitar/" rel="tag">guitar</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node-js/" rel="tag">node.js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/" rel="tag">protobuf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/recast/" rel="tag">recast</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/type/" rel="tag">type</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vscode/" rel="tag">vscode</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%90%E7%BB%B4/" rel="tag">运维</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/3D/" style="font-size: 10px;">3D</a> <a href="/tags/AI/" style="font-size: 11.67px;">AI</a> <a href="/tags/C/" style="font-size: 15px;">C++</a> <a href="/tags/Game/" style="font-size: 18.33px;">Game</a> <a href="/tags/Go/" style="font-size: 13.33px;">Go</a> <a href="/tags/Linux/" style="font-size: 13.33px;">Linux</a> <a href="/tags/Math/" style="font-size: 11.67px;">Math</a> <a href="/tags/Server/" style="font-size: 16.67px;">Server</a> <a href="/tags/Unreal-Engine4/" style="font-size: 10px;">Unreal Engine4</a> <a href="/tags/git/" style="font-size: 10px;">git</a> <a href="/tags/golang/" style="font-size: 20px;">golang</a> <a href="/tags/guitar/" style="font-size: 10px;">guitar</a> <a href="/tags/node-js/" style="font-size: 10px;">node.js</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/python/" style="font-size: 10px;">python</a> <a href="/tags/recast/" style="font-size: 11.67px;">recast</a> <a href="/tags/redis/" style="font-size: 10px;">redis</a> <a href="/tags/type/" style="font-size: 10px;">type</a> <a href="/tags/vscode/" style="font-size: 10px;">vscode</a> <a href="/tags/%E8%BF%90%E7%BB%B4/" style="font-size: 16.67px;">运维</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">April 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">March 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">January 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">November 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">November 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/19/2021/2021-07-19-etcd-learn/">go-etcd阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/07/19/2021/2021-07-19-socketop-learn/">REUSEPORT-REUSEADDR-学习</a>
          </li>
        
          <li>
            <a href="/2021/06/22/2021/2021-06-22-go-ethereum-learn/">go-ethereum阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/06/21/2021/2021-06-21-server%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/">server测试工具</a>
          </li>
        
          <li>
            <a href="/2021/06/09/2021/2021-06-09-pprof-doc/">go-pprof</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 Abel Sean<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>